package com.boge.activiti;

import org.activiti.engine.*;
import org.activiti.engine.impl.persistence.entity.VariableInstance;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Activiti7Test05 {

    /**
     * 流程部署操作
     */
    @Test
    public void test1(){
        // 1.获取ProcessEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        // 2.完成流程的部署操作 需要通过RepositoryService来完成
        RepositoryService repositoryService = processEngine.getRepositoryService();
        // 3.完成部署操作
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("flow/holiday1.bpmn20.xml")
                .name("请假流程-流程变量")
                .deploy(); // 是一个流程部署的行为 可以部署多个流程定义的
        System.out.println(deploy.getId());
        System.out.println(deploy.getName());
    }

    /**
     * 查询当前部署的流程有哪些
     */
    @Test
    public void test2(){
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = engine.getRepositoryService();
        // 查询有哪些部署的流程--》查询相关的流程定义信息
        // repositoryService.createDeploymentQuery() 查询流程部署的相关信息
        // repositoryService.createProcessDefinitionQuery() 查询部署的流程的相关的定义
        List<Deployment> list = repositoryService.createDeploymentQuery().list(); // 查询所有的部署信息
        for (Deployment deployment : list) {
            System.out.println(deployment.getId());
            System.out.println(deployment.getName());
        }

        List<ProcessDefinition> list1 = repositoryService.createProcessDefinitionQuery().list();
        for (ProcessDefinition processDefinition : list1) {
            System.out.println(processDefinition.getId());
            System.out.println(processDefinition.getName());
            System.out.println(processDefinition.getDescription());
        }
    }

    /**
     * 发起一个流程
     */
    @Test
    public void test3(){
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // 发起流程 需要通过 runtimeService来实现
        RuntimeService runtimeService = engine.getRuntimeService();
        // 定义流程变量信息
        Map<String,Object> map = new HashMap<>();
        map.put("assignee1","张三");
        map.put("assignee2","李四");
        map.put("ass1","变量1");
        map.put("ass2",299);
        map.put("ass3","湖南长沙");
        map.put("ass4","波哥666");
        // 通过流程定义ID来启动流程  返回的是流程实例对象
        ProcessInstance processInstance = runtimeService
                .startProcessInstanceById("holiday1:1:42503",map);
        System.out.println("processInstance.getId() = " + processInstance.getId());
        System.out.println("processInstance.getDeploymentId() = " + processInstance.getDeploymentId());
        System.out.println("processInstance.getDescription() = " + processInstance.getDescription());
    }

    /**
     * 查询当前的流程变量信息
     */
    @Test
    public void testVal(){
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = engine.getRuntimeService();
        // 获取流程变量信息  获取某个流程实例的变量信息
        Map<String, VariableInstance> variableInstances =
                runtimeService.getVariableInstances("40002");
        Set<String> keys = variableInstances.keySet();
        for (String key : keys) {
            System.out.println(key+"="+variableInstances.get(key));
        }
    }
    /**
     * 待办查询
     */
    @Test
    public void test4(){
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // 待办查询 执行中的任务处理通过 TaskService来实现
        TaskService taskService = engine.getTaskService();
        // Task 对象对应的其实就是 act_ru_task 这张表的记录
        List<Task> list = taskService.createTaskQuery().taskAssignee("lisi").list();
        if(list != null && list.size() > 0){
            for (Task task : list) {
                System.out.println(task.getId());
                System.out.println(task.getName());
                System.out.println(task.getAssignee());
            }
        }else{
            System.out.println("当前没有待办任务");
        }
    }

    /**
     * 任务审批
     */
    @Test
    public void test7(){
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // 做任务申请 也需要通过 TaskService 来实现
        TaskService taskService = engine.getTaskService();
        // 根据当前登录用户查询出对应的待办信息
        List<Task> list = taskService.createTaskQuery().taskAssignee("李四-boge3306").list();
        if(list != null && list.size() > 0){
            for (Task task : list) {
                // 做对应的任务审批处理
                taskService.complete(task.getId());
            }
        }
        // 完成任务
        // taskService.complete("2505");
    }
}
